package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.OrderStatusLogs;
import com.dingreading.cloud.admin.entity.table.OrderStatusLogsTableDef;
import com.dingreading.cloud.admin.mapper.OrderStatusLogsMapper;
import com.dingreading.cloud.admin.service.OrderStatusLogsService;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class OrderStatusLogsServiceImpl extends BaseServiceImpl<OrderStatusLogsMapper, OrderStatusLogs, OrderStatusLogsTableDef> implements OrderStatusLogsService {

    @Override
    protected OrderStatusLogsTableDef getTable() {
        return OrderStatusLogsTableDef.orderStatusLogs;
    }

    @Override
    public OrderStatusLogs getLastByOrderCode(String orderCode) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.id))
                .from(table)
                .where(table.orderCode.eq(orderCode));

        return getMapper().selectOneByCondition(table.orderCode.eq(orderCode).and(table.id.in(wrapper)));
    }

    @Override
    public List<OrderStatusLogs> lastListByOrderCode(List<String> orderCodes) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.id))
                .from(table)
                .where(table.orderCode.in(orderCodes))
                .groupBy(table.orderCode);

        return getMapper().selectListByCondition(table.orderCode.in(orderCodes).and(table.id.in(wrapper)));
    }

    @Override
    public List<OrderStatusLogs> listByOrderCode(String orderCode) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.orderCode.eq(orderCode))
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(wrapper);
    }

}
